<template>
  <div class="app-container">
    <el-card class="query-card">
      <el-form ref="query" label-width="80px" inline :model="page.query">
        <el-form-item label="操作时间" prop="dateRange">
          <el-date-picker
            v-model="page.query.dateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="page.query.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="ip地址" prop="ipAddr">
          <el-input v-model="page.query.ipAddr" placeholder="请输入ip地址" />
        </el-form-item>
        <el-form-item label="请求路径" prop="uri">
          <el-input v-model="page.query.uri" placeholder="请输入请求路径" />
        </el-form-item>
        <el-form-item label="方法名" prop="methodName">
          <el-input v-model="page.query.methodName" placeholder="请输入方法名" />
        </el-form-item>
        <el-form-item label="是否异常" prop="errorFlag">
          <dict-select v-model="page.query.errorFlag" dict-key="true_or_false" placeholder="请选择是否异常" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQueryForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <!--      <div slot="header">
        <span>操作日志信息列表</span>
      </div>-->
      <div class="table-button">
        <el-button type="danger" class="el-icon-folder-delete" :disabled="selected.length<1" @click="handleBatchDelete">批量删除
        </el-button>
        <el-tooltip content="刷新" class="table-right-btn">
          <el-button size="mini" circle icon="el-icon-refresh" @click="doPage" />
        </el-tooltip>
      </div>
      <el-table
        v-loading="tableLoading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="45" />
        <el-table-column label="用户名" prop="username" min-width="100" />
        <el-table-column label="ip地址" prop="ip" min-width="120" />
        <el-table-column label="ip归属地" prop="address" min-width="180" />
        <el-table-column label="请求Api路径" prop="uri" min-width="180" show-overflow-tooltip />
        <el-table-column label="方法名" prop="methodName" min-width="100" show-overflow-tooltip />
        <el-table-column label="耗时(ms)" prop="executionTime" min-width="80" />
        <el-table-column label="是否异常" prop="errorFlag" min-width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.errorFlag==='1'" type="danger">是</el-tag>
            <el-tag v-else type="success">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" prop="requestTime" min-width="160" show-overflow-tooltip />
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="detailClick(scope.row)"
            >详情
            </el-button>
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              placement="top"
              style="margin-left: 10px"
              title="是否删除此条数据"
              @confirm="handleDelConfirm(scope.row)"
            >
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="dialogVisible">
      <detail-module ref="detailModule" :visible="dialogVisible" @close="handleClose" />
    </div>
  </div>
</template>

<script>
import { deleteLog, selectPage } from '@/api/system/log'
import detailModule from './modules/detailModule.vue'

export default {
  name: 'Index',
  components: { detailModule },
  data() {
    return {
      tableLoading: false,
      dialogTitle: '',
      selected: [],
      tableData: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          userId: '',
          username: '',
          ipAddr: '',
          dateRange: []
        }
      },
      dialogVisible: false,
      submitForm: {}
    }
  },
  mounted() {
    this.doPage()
  },
  methods: {
    handleSearch() {
      this.page.page = 1
      this.doPage()
    },
    doPage() {
      this.tableLoading = true
      selectPage(this.page).then(resp => {
        if (resp.success) {
          this.tableData = resp.data.records
          this.page.total = resp.data.total
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    handleBatchDelete() {
      if (this.selected.length === 0) {
        this.$message.warning('请选择一条或多条数据')
        return
      }
      this.$confirm('是否批量删除选中的数据?', '删除提醒', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const data = this.selected.map(e => e.id)
        this.tableLoading = true
        deleteLog({ ids: data }).then(resp => {
          if (resp.success) {
            this.$message.success('删除成功')
            this.doPage()
          }
        }).finally(() => {
          this.tableLoading = false
        })
      })
    },
    detailClick(row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.detailModule.init(row.id)
      })
    },
    closeSave(val) {

    },
    handleSelectionChange(row) {
      this.selected = row
    },
    resetQueryForm() {
      this.$refs.query.resetFields()
      this.doPage()
    },
    // 分页大小
    handleSizeChange(val) {
      this.page.size = val
      this.doPage()
    },
    // 页码
    handleCurrentChange(val) {
      this.page.page = val
      this.doPage()
    },
    handleClose() {
      this.dialogVisible = false
    },
    handleDelConfirm(row) {
      this.tableLoading = true
      const data = { ids: [row.id] }
      deleteLog(data).then(resp => {
        if (resp.success) {
          this.$message.success('删除成功')
          this.doPage()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
